package org.linlinjava.litemall.taobaoke.sdk.impl;

import com.alibaba.fastjson.JSON;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkDgMaterialOptionalRequest;
import com.taobao.api.request.TbkDgOptimusMaterialRequest;
import com.taobao.api.request.TbkJuTqgGetRequest;
import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import com.taobao.api.response.TbkDgOptimusMaterialResponse;
import com.taobao.api.response.TbkJuTqgGetResponse;
import org.linlinjava.litemall.core.exception.SystemException;
import org.linlinjava.litemall.core.result.StatusCode;
import org.linlinjava.litemall.taobaoke.DTO.MaterialFeaturedQueryDTO;
import org.linlinjava.litemall.taobaoke.DTO.MaterialQueryDTO;
import org.linlinjava.litemall.taobaoke.DTO.TqgQueryDTO;
import org.linlinjava.litemall.taobaoke.sdk.MaterialDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author haoting.wang
 * @date 2019-07-26 16:58
 **/
@Component
public class TaobaokeMaterialDao implements MaterialDao {

    public final static String SERVER_URL = "http://gw.api.taobao.com/router/rest";

    public final static String APP_KEY = "27712257";

    public final static String APP_SECRET = "274447c0dc6dae073e7a32291cf259b2";

    public final static Long ADZONE_ID = 109202950230L;

    public final static Long SITE_ID = 649700016L;

    public final static String PID = "mm_109504135_649700016_109202950230";

    public final static String TQG_FIELDS="click_url,pic_url,reserve_price,zk_final_price,total_amount,sold_num,title,category_name,start_time,end_time";

    public final static Logger LOGGER = LoggerFactory.getLogger(TaobaokeMaterialDao.class);


    @Override
    public List<TbkDgMaterialOptionalResponse.MapData> query(MaterialQueryDTO materialQueryDTO) {


        TaobaoClient client = new DefaultTaobaoClient(SERVER_URL, APP_KEY, APP_SECRET);
        TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();


        TbkDgMaterialOptionalResponse rsp = null;
        try {
            BeanUtils.copyProperties(materialQueryDTO,req);
            req.setAdzoneId(ADZONE_ID);

            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new SystemException(StatusCode.SYSTEM_ERROR, e);
        }


        return rsp.getResultList();
    }


    @Override
    public List<TbkDgOptimusMaterialResponse.MapData> queryFeatured(MaterialFeaturedQueryDTO materialFeaturedQueryDTO) {


        TaobaoClient client = new DefaultTaobaoClient(SERVER_URL, APP_KEY, APP_SECRET);
        TbkDgOptimusMaterialRequest req = new TbkDgOptimusMaterialRequest();


        TbkDgOptimusMaterialResponse rsp = null;
        try {
            BeanUtils.copyProperties(materialFeaturedQueryDTO,req);
            req.setAdzoneId(ADZONE_ID);

            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new SystemException(StatusCode.SYSTEM_ERROR, e);
        }

        return rsp.getResultList();
    }


    @Override
    public List<TbkJuTqgGetResponse.Results> queryTqg(TqgQueryDTO tqgQueryDTO) {

        TaobaoClient client = new DefaultTaobaoClient(SERVER_URL, APP_KEY, APP_SECRET);
        TbkJuTqgGetRequest req = new TbkJuTqgGetRequest();

        TbkJuTqgGetResponse rsp = null;
        try {
            BeanUtils.copyProperties(tqgQueryDTO,req);
            req.setAdzoneId(ADZONE_ID);
            req.setFields(TQG_FIELDS);

            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new SystemException(StatusCode.SYSTEM_ERROR, e);
        }
        return rsp.getResults();

    }
}
